package com.api.rest.exception.resolver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by dan3652@sina.com on 2016-12-02 10:49.
 */
public class RestExceptionResolver implements HandlerExceptionResolver {
    private static final Logger logger = LoggerFactory
            .getLogger(RestExceptionResolver.class);

    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object obj, Exception exception) {
        ModelAndView modelAndView = new ModelAndView();
        logger.error(exception.getMessage(), exception);
        modelAndView.addObject("errcode", "40009");
        if(exception instanceof AuthFailException) {
            modelAndView.addObject("errmsg", exception.getMessage());
        } else {
            modelAndView.addObject("errmsg", "参数错误："+exception.getMessage());
        }
        return modelAndView;
    }
}
